package com.css.apps.base.user.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.common.provider.OrgQueryProvider;
import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.post.model.SPost;
import com.css.apps.base.user.model.SUser;
import com.css.db.page.Page;
import com.css.db.query.QueryCache;
import com.css.util.Messages;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

@SuppressWarnings("serial")
public class GetUserPost extends AdminAction {
	
	private static Log log = LogFactory.getLog(GetUserPost.class);
	private String orgId;
	private String userId;
	private Page page;
	private List<SPost> PostList;

	public GetUserPost() {
		page = new Page();
		page.setCurrentPage(1);
		page.setPageSize(5);
		page.setCountField("a.uuid");
	}

	@SuppressWarnings("unchecked")
	@Override
	protected String adminGo() {
		try {
			SUser sUser = QueryCache.get(SUser.class, userId);
			if (sUser == null) {
				setMessage(Messages.getString("systemMsg.failed"));
				return Action.ERROR;
			}
			List<SOrg> userOrgs = OrgQueryProvider.queryOrgsByUserId(userId);
			List<String> orgPostIdList = new ArrayList<>();
			for (SOrg org : userOrgs) {
				orgPostIdList.addAll(QueryCache.get(SOrg.class, org.getUuid()).getPostList().getListById());
			}
			List<String> postIdList = new ArrayList<String>();
			if (orgPostIdList.size() == 0) {
				setMessage(Messages.getString("systemMsg.NoAddOrgPost"));
				return Action.ERROR;
			}
			for (String ids : orgPostIdList) {
				postIdList.add(ids.trim());
			}
			List<String> userPostList = sUser.getPostList().getListById();
			for (String ids : userPostList) {
				postIdList.remove(ids.trim());
			}
			PostList = QueryCache.idToObj(SPost.class, postIdList);
			if (PostList.size() == 0) {
				setMessage(Messages.getString("systemMsg.NoAddPost"));
				return Action.ERROR;
			}
			// int totalrows = PostList.size();
			// int totalpages = (totalrows/page.getPageSize())+1;
			// int begin = (page.getCurrentPage()-1)*page.getPageSize();
			// int end = begin + page.getPageSize();
			// if(end > PostList.size()){
			// page.setResults(PostList.subList(begin, PostList.size()));
			// }else{
			// page.setResults(PostList.subList(begin, end));
			// }
			// page.setTotalRows(PostList.size());
			// page.setTotalPages(totalpages);
			page.setResults(PostList);
			// 设置分页
			loadPage(page);
			return SUCCESS;
		} catch (Exception ex) {
			setMessage(Messages.getString("systemMsg.exception"));
			log.error(ex.getMessage(), ex);
			return Action.ERROR;
		}
	}

	private void loadPage(Page page) {
		List<?> results = page.getResults();
		if (results != null && results.size() > 0) {
			page.setTotalRows(results.size());
			page.setTotalPages((int) Math.ceil(results.size() * 1.0 / page.getPageSize()));
			int total = results.size();
			int curPage = page.getCurrentPage();
			int pageSize = page.getPageSize();
			if (curPage > page.getTotalPages()) {
				curPage = page.getTotalPages();
			}
			int startIndex = (curPage > 0 ? curPage - 1 : 0) * pageSize;
			if (startIndex > total) {
				startIndex = 0;
			}
			int endIndex = startIndex + pageSize;
			if (endIndex > total) {
				endIndex = total;
			}
			results = results.subList(startIndex, endIndex);
			page.setResults(results);
		}
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<SPost> getPostList() {
		return PostList;
	}

	public void setPostList(List<SPost> postList) {
		PostList = postList;
	}

	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

}
